package com.xy.netutils;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.xy.netutils.base.BaseActivity;
import com.xy.netutils.bean.NewsData;
import com.xy.netutils.net.BaseSubscriber;
import com.xy.netutils.net.HttpResultFunc;
import com.xy.netutils.net.NetUtils;

import java.util.List;

public class MainActivity extends BaseActivity {

    private ListView listView;
    private NewsAdapter newsAdapter;
    private List<NewsData> result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.list);
        newsAdapter = new NewsAdapter();
        listView.setAdapter(newsAdapter);
    }

    public void startRequest(View v) {
        String key = "替换成你自己的key"; //你也可以改成自己的后台接口！
        String q = "支付鸨";
        NetUtils.toSubscribe(getNetService().getNewsData(key, q).map(new HttpResultFunc<List<NewsData>>()), new BaseSubscriber<List<NewsData>>(MainActivity.this) {
            @Override
            public void onNext(List<NewsData> o) {
                result = o;
                newsAdapter.notifyDataSetChanged();
            }
        });

    }



    class NewsAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return result == null ? 0 : result.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
            TextView tiltle = (TextView) view.findViewById(R.id.title);
            TextView content = (TextView) view.findViewById(R.id.content);
            tiltle.setText(result.get(i).getTitle());
            content.setText(result.get(i).getContent());
            return view;
        }
    }
}
